package com.ypc.webdemo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import static com.google.common.base.Predicates.not;
import static springfox.documentation.builders.PathSelectors.regex;

@Configuration
@ConditionalOnExpression("${swagger.enable:true}")
public class SwaggerConfig {
    @Value("${info.app.version:0.0.1}")
    private String version;
    @Value("${spring.application.name}")
    private String applicationName;
    @Value("${info.app.description:描述}")
    private String description;

    ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(applicationName)
                .description(description)
                .version(version)
                .build();
    }

    @Bean
    public Docket customImplementation() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ypc.webdemo"))
                .paths(not(regex("/")))
                .build()
                .apiInfo(apiInfo());
    }
}
